package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 上饶房产
 * @author Persh
 *
 */
public class ZzsrNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.zzsr.com/zxContent/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.html",
		"http://www.zzsr.com/tudi/tdwzxContent/[0-9]*.html"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h2").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("td.table1").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("div.tit b").text();
			}
		}
		if(StringUtils.isBlank(title)){
			if(doc.select(".article p").size() > 1){
				title=doc.select(".article p").get(0).text();
				author=StringUtils.substringBetween(doc.select(".article p").get(1).text(), "来源：", " 时间：");
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.neirong"));
		if(StringUtils.isBlank(content)){	
			content=HtmlCleaner.getContentHtml(url,doc.select("div.article"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.con"));
			}
		}
		date=DateUtils.matchDate(doc.select("h3").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("p[align=right]").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.tit").text());
				if(date==null){
					date=DateUtils.matchDate(url);
				}
			}
		}
		
		if(date == null){
			date=DateUtils.matchDate(doc.select("p[style=font-size:12px;]").text());
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("span[style=font-size:14px;]"));
		}
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setAuthor(author);
		
		return news;
	}
	
	public static void main(String[] args) {
		ZzsrNewsAnalyse analyse=new ZzsrNewsAnalyse();
		String url="http://www.zzsr.com/tudi/tdwzxContent/201403302154380.html";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
